/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.timerTask;

import cn.ac.iie.di.wx.mq.common.RuntimeEnv;
import cn.ac.iie.di.wx.mq.httpservice.HttpSender;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.TimerTask;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class RedoTask extends TimerTask {

    static Logger logger = Logger.getLogger(RedoTask.class.getName());

    @Override
    public void run() {
        //读取文件
        //获取目录下文件，及其遍历
        String root = System.getProperty("user.dir");
        File dir = new File(root + "/" + "redo/");
        File[] files = dir.listFiles();

        if (files.length > 0) {
            for (File file : files) {
                String fileName = file.getName();
                if (fileName.contains(".ok")) {
                    long starttime = System.currentTimeMillis();
                    logger.info("start redo fileName = "+fileName);
                    FileReader reader = null;
                    try {
                        //已经完成的文件
                        String str = "";
                        reader = new FileReader(root + "/" + "redo/" + fileName);
                        BufferedReader br = new BufferedReader(reader);
                        while ((str = br.readLine()) != null) {
                            String[] vos = str.split("|");
                            //放入redids队列
                            //重新发送
                            //重新放入队列入redis，发送烽火 String result =
                            String result = HttpSender.sendStringByUrl(vos[2], (String) RuntimeEnv.getParam(RuntimeEnv.groupMediaQuery));
//                            String result = "{\"opid\":\"13344030018812013256\",\"frontnum\":0,\"status\":\"200\",\"description\":\"下载成功。\"}";
                            boolean flag = false;
                            if (!result.equals("")) {
                                JSONObject jSONObject = JSONObject.parseObject(result);
                                if (jSONObject.getString("status").equals("200")) { //操作成功 flag = true;
                                    logger.info("RedoTask groupMedia response is:" + result);
                                    RuntimeEnv.GMCount.getAndIncrement();
                                    flag = true;
                                }
                            }
                            if (!flag) {
                                while (true) {
                                    try {
                                        if (RuntimeEnv.retryQueue.offer(new RedisVo(vos[0], vos[1], vos[2]))) {
                                            break;
                                        }
                                        Thread.sleep(200);
                                    } catch (Exception e) {
                                        logger.error(e.getMessage(), e);
                                    }
                                }
                            }

                        }
                    } catch (FileNotFoundException ex) {
                        logger.error(ex.getMessage(), ex);
                    } catch (IOException ex) {
                        logger.error(ex.getMessage(), ex);
                    } finally {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            logger.error(e.getMessage(), e);
                        }
                    }
                    logger.info("finish redo fileName = "+fileName+",use time = "+(System.currentTimeMillis()-starttime)+" ms ");
                    File finishFile=new File(dir+"/"+fileName.replace("ok", "suc"));
                    file.renameTo(finishFile);
                    logger.info("finished redo fileName = "+finishFile.getName());
                }
            }
        }
    }

}
